<div class="content-wrapper">
	<section class="content-header">
		<h1>
			设置用户组权限
			<small> 【{$userGroupInfo.name}】 已开通的功能使用<span style='color: red;'>红色</span>标识</small>
		</h1>
		<ol class="breadcrumb">
			<li><a href="/admin"><i class="fa fa-dashboard"></i> 首页</a></li>
			<li class="active">权限管理</li>
			<li class="active">设置用户组权限</li>
		</ol>
	</section>
	<section class="content">
		<div class="box">
			<div class="box-header with-border">
				<input type="hidden" name="user_group_id" id="user_group_id" value="{$userGroupInfo.id}" />
				<button type="button" class="btn btn-info" id="save-btn">保存用户组权限</button>
			</div>
			<div class="box-body">
				<table class="table table-bordered">                            
                    <tbody>
                    	<foreach name="functionGroupList" item="functionGroup">
                    		<tr>
                    			<td>
                    				<div id="groupsids_{$functionGroup.id}" class="accordion-group">
                    					<label style="width:250px;" class="position-relative">
                    						<input type="checkbox" name="group_ids[]" value="{$functionGroup.id}">
                    						<span title="{$functionGroup.name}">
                    							<b>{$functionGroup.name}</b>
                    						</span>
                    					</label>
                    				</div>
                    				<div id="fun_ids_{$functionGroup.id}" class="accordion-inner" style="padding:9px 15px 9px 30px;">
                    					<foreach name="functionGroup.functionList" item="function">
                    						<label  style="width:250px;" class="position-relative">
                    							<input type="checkbox" name="function_ids[]" value="{$function.id}" <if condition="$function.is_checked eq 1">checked</if>  tag="{$functionGroup.id}">
                    							<span style="font-weight:normal;<if condition='$function.is_checked eq 1'>color:#FF0000;</if>" title="{$function.path}">{$function.name}</span>
                    						</label>
                    					</foreach>
                    				</div>
                    			</td>
                    		</tr>
                    	</foreach>
                    </tbody>
                </table>									
			</div>			
		</div>
	</section>
</div>

<script type="text/javascript">
$(function() {
	$("input:checkbox[name='group_ids[]']").click(function() {
	    var pgid = this.value;
	    if (this.checked) {
	        $("#fun_ids_"+pgid).find("input:checkbox").prop("checked", true);
	    } else {
	        $("#fun_ids_"+pgid).find("input:checkbox").prop("checked", false);
	    }
	});
	
	$("input:checkbox[name='func_ids[]']").click(function() {
	    var gid_tag = $(this).attr('tag');    
	    var pgElem = $("#groupsids_"+gid_tag).find("input[name='group_ids[]']")[0];    
	    if (this.checked && !pgElem.checked) { // 判断是否要勾上全选
	        if (0 == $(this).parents(".accordion-inner").find("input:checkbox").not("input:checked").length) {
	            pgElem.checked = true;
	        }
	    } else if (!this.checked && pgElem.checked) { // 判断是否要取消全选
	        if ($(this).parents(".accordion-inner").find("input:checkbox:checked").length) {
	            pgElem.checked = false;
	        }
	    }
	});

	$("#save-btn").click(function() {
		var user_group_id = $("#user_group_id").val();
		var functionIdList = [];
		$('input[name="function_ids[]"]:checked').each(function() {
			var function_id = $(this).val();
			functionIdList.push(function_id);
		});

		$.post('/admin/auth/saveUserGroupFunction', {
			user_group_id : user_group_id,
			function_ids : functionIdList.join(',')
		}, function(data) {
			if (data.Code == 999) {
				ArtDialogHlp.showInfo('提示', '操作成功', function() {
					window.location.reload();
				});
			} else {
				ArtDialogHlp.showInfo('提示', data.Msg);
			}
		}, 'json');
	});
});
</script>